package com.hzjq.fxlib.presenter

import android.graphics.drawable.GradientDrawable
import android.view.KeyEvent
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import com.hzjq.core.bean.ErrorCapEntity
import com.hzjq.fxlib.R
import com.hzjq.fxlib.ui.ErrorCapActivity
import com.wxl.common.util.DPUtil
import kotlinx.android.synthetic.main.layout_blast_error.view.*

class BlastErrorPresenter : AbsViewPresenter {
    private var isEnd = false
    private var errorCap = arrayListOf<ErrorCapEntity>()

    constructor(rootView: ViewGroup) : super(rootView)

    override fun onContentViewId(): Int {
        return R.layout.layout_blast_error
    }

    override fun onResume() {

    }

    override fun showAction(action: String) {
        rootView.blastErrorText.text = action
        (rootView.blastErrorLayout.background as GradientDrawable).setColor(ContextCompat.getColor(getContext(),android.R.color.holo_red_dark))
        (rootView.blastErrorLineView.background as GradientDrawable).setStroke(DPUtil.dip2px(2),ContextCompat.getColor(getContext(),android.R.color.holo_red_dark))
    }

    fun isScanEnd(b: Boolean, errorCap: ArrayList<ErrorCapEntity>) {
        this.isEnd = b
        this.errorCap.clear()
        this.errorCap.addAll(errorCap)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if(isEnd && (keyCode == KeyEvent.KEYCODE_F1 || keyCode == KeyEvent.KEYCODE_F5) && errorCap.isNotEmpty()){
            ErrorCapActivity.start(getContext(),errorCap)
            return true
        }
        return super.onKeyDown(keyCode, event)
    }
}